<?php
class publish_log extends Model{
	public static function getTableName(){
		return 'publish_log';
	}

	public static function saveLog($action,$table_name,$id,$time=null,$user=null,$item_obj=null){
        if ($id < 1 )   return false;

        switch ($action) {
        case AC::LOG_OP_PUBLISH:
            $status = AC::STATUS_PUBLISH;
            break;
        case AC::LOG_OP_UNPUBLISH:
            $status = AC::STATUS_UNPUBLISH;
            break;
        case AC::LOG_OP_PREMIUM_PUBLISH:
            $status = AC::STATUS_PUBLISH_PREMIUM;
            break;
        case AC::LOG_OP_PREMIUM_UNPUBLISH:
            $status = AC::STATUS_UNPUBLISH_PREMIUM;
            break;
		case AC::LOG_OP_PUBLISHING:
		case AC::LOG_OP_UNPUBLISHING:
			$status = AC::STATUS_PROCESSING;
            break;
		case AC::LOG_OP_PREMIUM_PUBLISHING:
		case AC::LOG_OP_PREMIUM_UNPUBLISHING:
			$status = AC::STATUS_PROCESSING_PREMIUM;
            break;
        }

		$pub_obj  = new publish_log();
		if(empty($item_obj) || !($item_obj instanceof item_type)) {
			$item_obj = new item_type();
		}
		$src_data = Model::object2array(Model::$dbo['r']->findInstance($table_name,$id));
	
		$pub_obj->item_type_id = $item_obj->getItemIDByName($table_name);
		$pub_obj->item_id      = $id;
		$pub_obj->published_at = $time ? $time : date('Y-m-d H:i:s');
		$pub_obj->published_by = $user ? $user : CommonUtils::get_user();
		$pub_obj->status       = $status;
		$pub_obj->action       = $action;
		$pub_obj->data         = json_encode($src_data);
		$pub_obj->remarks      = AC::$op_status[$status];

		return Model::$dbo['w']->save($pub_obj);
	}

	/*
	public static function getPublishLogDetail($type, $id)
	{
        $itemTypeId = item_type::getItemIDByName($type);

        $sql = "
			SELECT * 
			FROM publish_log
			WHERE id=(
				select max(id) 
				from publish_log 
				where item_type_id={$itemTypeId} and item_id={$id} and (status=" . AC::STATUS_PUBLISH . ")
			) 
            ";
		$result = Model::$dbo['r']->findBySql(__class__, $sql);

		return !empty($result[0]) ? $result[0] : FALSE;
	}

	public static function getPremiumPublishLogDetail($type, $id)
	{
        $itemTypeId = item_type::getItemIDByName($type);

        $sql = "
			SELECT * 
			FROM publish_log
			WHERE id=(
				select max(id) 
				from publish_log 
				where item_type_id={$itemTypeId} and item_id={$id} and (status=" . AC::STATUS_PUBLISH_PREMIUM . ")
			) 
            ";
		$result = Model::$dbo['r']->findBySql(__class__, $sql);

		return !empty($result[0]) ? $result[0] : FALSE;
	}*/

    public static function getPublishLog($type, $id)
    {
		$return = array();
        $itemTypeId = item_type::getItemIDByName($type);
        $sql = "
            SELECT * FROM publish_log
            WHERE id=(select max(id) from publish_log where item_type_id={$itemTypeId} and item_id={$id} and (status=".AC::STATUS_PUBLISH." or status=".AC::STATUS_UNPUBLISH." or status=".AC::STATUS_PROCESSING." or status=".AC::STATUS_LOG_FAILED.")) 
            UNION
            SELECT * FROM publish_log
            WHERE id=(select max(id) from publish_log where item_type_id={$itemTypeId} and item_id={$id} and (status=".AC::STATUS_PUBLISH_PREMIUM." or status=".AC::STATUS_UNPUBLISH_PREMIUM." or status=".AC::STATUS_PROCESSING_PREMIUM." or status=".AC::STATUS_LOG_FAILED_PREMIUM.")) 
        ";

		$result = Model::$dbo['r']->findBySql(__class__, $sql);
        if ($result) {
			#error_log(print_r($result,1));
            foreach ($result as $object) {
                $object->published_at = str_replace(' ', '<br />', $object->published_at);
                switch ($object->status) {
					case AC::STATUS_PUBLISH:
					case AC::STATUS_UNPUBLISH:
					case AC::STATUS_PROCESSING:
						$return['non_premium'] = $object;
					break;
					case AC::STATUS_PUBLISH_PREMIUM:
					case AC::STATUS_UNPUBLISH_PREMIUM:
					case AC::STATUS_PROCESSING_PREMIUM:
						$return['premium'] = $object;
					break;
                }
            }
			#error_log(print_r($return,1));
        }
        return (object) $return;
    }

    public static function getNonPremiumPublishLog($type, $id)
    {
        $itemTypeId = item_type::getItemIDByName($type);

        $sql = "
            SELECT * FROM publish_log
            WHERE id=(select max(id) from publish_log where item_type_id={$itemTypeId} and item_id={$id} and (status=" . AC::STATUS_PUBLISH . " or status=" . AC::STATUS_UNPUBLISH . ")) 
            ";
		$result = Model::$dbo['r']->findBySql(__class__, $sql);

		return !empty($result) ? $result['0'] : FALSE;
    }

    public static function getPremiumPublishLog($type, $id)
    {
        $itemTypeId = item_type::getItemIDByName($type);

        $sql = "
            SELECT * FROM publish_log
            WHERE id=(select max(id) from publish_log where item_type_id={$itemTypeId} and item_id={$id} and (status=".AC::STATUS_PUBLISH_PREMIUM." or status=".AC::STATUS_UNPUBLISH_PREMIUM.")) 
            ";
		$result = Model::$dbo['r']->findBySql(__class__, $sql);
        if (empty($result)) {
            return FALSE;
        } else {
            return $result['0'];
        } 
    }

	public static function checkVideosNotPublished(array $video_ids)
	{
        $item_type_id = item_type::getItemIDByName('video');
		$sql = '
			SELECT COUNT(DISTINCT(item_id)) AS total
			FROM publish_log
			WHERE item_type_id=' . $item_type_id . ' and item_id in (' . implode(',', $video_ids) . ') and (status=' . AC::STATUS_PUBLISH . ' or status=' . AC::STATUS_UNPUBLISH . ')
			';
		$result = Model::selectBySql($sql);
		return ($result[0]['total'] == count($video_ids)) ? FALSE : TRUE;
	}

}
?>
